#### This script will match two very large microclimate datasets by site, date, and time
#### Then need to interrogate for differences in time/magnitude of max temps between ambient and under log data loggers
#### Set up a directory

raw.dir = '/home1/99/jc152199/underlogdownscale/rawmicroclimatedata/'

#### First read in some raw air data .csvs

airraw = read.csv(paste(raw.dir,'airtemprawdata.csv',sep=''),header=T)

hoboraw = read.csv(paste(raw.dir,'hoboairtemprawdata.csv',sep=''),header=T)

### Now rowbind them

airraw = rbind(hoboraw,airraw)

### Remove hoboraw from memory

rm('hoboraw')

### Read in underlog rawdata

ulraw = read.csv(paste(raw.dir,'underlograwdata.csv',sep=''),header=T)

### Merge

microraw = merge(airraw, ulraw, by=c('site','date','hour'))

#### Remove ulraw and airraw from memory

rm('ulraw')
rm('airraw')

#### Write out microraw

write.csv(microraw, file=paste(raw.dir,'ulandairrawdata.csv',sep=''), row.names=F)

#### Remove superfluous columns

microraw2 = microraw[,c(1:11,18:19)]

#### Fix some names for clarity

names(microraw2) = gsub('.x','',names(microraw2))
names(microraw2)[10]='airminutes'
names(microraw2)[12]='ulminutes'

### Write out microraw2

write.csv(microraw2, file=paste(raw.dir,'ulandairrawdata_namesfixed_columnsremoved.csv'), row.names=F)

#### Remove microraw

rm('microraw')

#### Find NA's in airtemp and ultemp and remove them

microraw3 = microraw2[which(is.na(microraw2$airtemp)==F & is.na(microraw2$ultemp)==F),]

#### Write out microraw3

write.csv(microraw3, file=paste(raw.dir,'ulandairrawdata_namesfixed_columnsremoved_NAsremoved.csv'), row.names=F)

r = read.csv(paste(raw.dir,'ulandairrawdata_namesfixed_columnsremoved_NAsremoved.csv'), header=T)

### Clear memory and shorten object names

rm('microraw2')
r = microraw3
rm('microraw3')

### Aggregate to produce daily max for ultemp and airtemp

ulag = aggregate(r[,c(11,13)], by=list(site=r$site,date=r$date), FUN=max)
ulag2 = aggregate(r[,c(11,13)], by=list(site=r$site,date=r$date), FUN=min)

### Change some names

names(ulag)[3] = 'airmax'
names(ulag)[4] = 'ulmax'

### Add data to ulag

ulag$airmin = ulag2[,3]
ulag$ulmin = ulag2[,4]



#### Write out ulag

write.csv(ulag, file=paste(raw.dir,'ulag.csv',sep=''), row.names=F)


